var buildJsonStringForTree = function (obj, jsonString, lvl, parentIsArray) {
	if (!lvl) lvl = 0;

	lvl++;

	var ind = null;
	var counter = 1;

	var nextLine = "\n";
	var space = "\t"
	var tab = "";
	for (var i = 0; i < lvl; i++)
		tab += space;

	for (ind in obj) {

		if (ind != "remove")
		{
			var text = ind;

			var itemValueType = "";
			var qTip = "";

			jsonString += tab + "{" + nextLine;
			jsonString += tab + space + '"id" : "' + ind + '_' + lvl + '_' + counter + '",' + nextLine;
			
			if ( isObject(obj[ind]) && obj[ind] != null )
			{
				if (parentIsArray)
					text = "[object Object] #" + ind;

				itemValueType = ( isArray(obj[ind]) ? "array" : "object" );

				jsonString += tab + space + '"leaf" : false,' + nextLine;
				jsonString += tab + space + '"type" : "' + itemValueType  + '",' + nextLine;
				jsonString += tab + space + '"iconCls" : "ico_' + itemValueType  + '",' + nextLine;
				jsonString += tab + space + '"expandable" : true,' + nextLine;
				//jsonString += tab + space + '"singleClickExpand" : true,' + nextLine;

				//jsonString += tab + '"listeners": {"beforeexpand": function (child) { child.iconCls = "ico_' + itemValueType  + '_active"}},';

				jsonString += tab + space + '"children" : [' + nextLine;

				jsonString = buildJsonStringForTree(obj[ind], jsonString, lvl, (itemValueType == "array"));

				jsonString += tab + space + '],' + nextLine;

				qTip += '<div class=\'tree-qtip-cell-caption\'><b>Key:</b></div>' + text  + '<br style=\'clear: both;\'/>';
				qTip += '<div class=\'tree-qtip-cell-caption\'><b>Typ:</b></div>' + itemValueType;
			}
			else
			{
				if (parentIsArray)
					text = obj[ind];

				if (obj[ind] == null)
					itemValueType = "null";

				if ( isString(obj[ind]) )
					itemValueType = "string";

				if ( !isNaN(obj[ind]) && !isString(obj[ind]) && obj[ind] != null )
					itemValueType = "number";

				

				qTip += '<div class=\'tree-qtip-cell-caption\'><b>Key:</b></div>' + text  + '<br style=\'clear: both;\'/>';
				qTip += '<div class=\'tree-qtip-cell-caption\'><b>Value:</b></div>' + obj[ind]  + '<br style=\'clear: both;\'/>';
				qTip += '<div class=\'tree-qtip-cell-caption\'><b>Typ:</b></div>' + itemValueType;


				jsonString += tab + space + '"draggable" : true,' + nextLine;
				jsonString += tab + space + '"leaf" : true,' + nextLine;
				jsonString += tab + space + '"value" : "' + obj[ind] + '",' + nextLine;
				jsonString += tab + space + '"type" : "' + itemValueType  + '",' + nextLine;
			}

			jsonString += tab + space + '"qtip" : "' + qTip + '",' + nextLine;
			jsonString += tab + space + '"text" : "' + text + '"' + nextLine;
			jsonString += tab + "}";

			if (counter < count(obj))
				jsonString += ",";

			jsonString += nextLine;

			counter++;
		}
	}
	return jsonString;
};


var buildJSONStringFromTree = function ( node, jsonString, lvl ) {
	if (!lvl) lvl = 0;

	lvl++;

	var tab = "";
	for (var i = 1; i < lvl; i++)
		tab += "\t";

	var collapseNode = false;
	if ( node.isExpandable() && !node.isExpanded() )
	{
		collapseNode = true;
		node.expand();
	}

	node.eachChild(function (child) {
		if ( child.hasChildNodes() )
		{
			if ( child.attributes.type == "array" )
				jsonString += tab + '\t"' + child.attributes.text + '": [\n';
			else
			{
				jsonString += tab + '\t';
				
				if ( node.attributes.type != "array" )
					jsonString += '"' + child.attributes.text + '": ';

				jsonString += '{\n';
			}

			jsonString = tab + buildJSONStringFromTree ( child, jsonString, lvl );

			if ( child.attributes.type == "array" )
				jsonString += tab + '\t]';
			else
				jsonString += tab + '\t}';

			jsonString += ( !child.isLast() ? "," : "" ) + '\n';
		}
		else
		{
			var nodeValue = child.attributes.value;

			if ( child.attributes.type == "string" )
				nodeValue = '"' + nodeValue + '"';


			jsonString += tab + '\t';
			
			if ( node.attributes.type == "array" )
				jsonString += nodeValue;
			else
				jsonString += '"' + child.attributes.text + '": ' + nodeValue;

			jsonString += ( !child.isLast() ? "," : "" ) + '\n';
		}
	});

	if ( collapseNode )
		node.collapse();

	return jsonString;
};



var saveJsonStringToTree = function() {
	var value = Ext.getCmp("JSONxEdit_FormPanel_TextArea_jsonString").getValue();

	try
	{
		var jsonObject = Ext.util.JSON.decode(value);
	}
	catch (e)
	{
		//Ext.Msg.alert('ERROR', "Fehler beim Parsen des JSON Strings.");
		return;
	}

	var rootType = "";

	if ( isObject(jsonObject) )
		rootType = "object";

	if ( isArray(jsonObject) )
		rootType = "array";

	var jsonStringForTree = "[\n";
	jsonStringForTree = buildJsonStringForTree(jsonObject, jsonStringForTree, 0, (rootType == "array"));
	jsonStringForTree += "]";
debug.trace("--"+jsonStringForTree+"--");
	try
	{
		var jsonTreeObject = Ext.util.JSON.decode(jsonStringForTree);
	}
	catch (e)
	{
		//Ext.Msg.alert('ERROR', "Fehler beim Parsen des JSON Strings.");
		return;
	}


	var qTip = '<div class=\'tree-qtip-cell-caption\'><b>Key:</b></div><b>ROOT</b><br style=\'clear: both;\'/>';
	qTip += '<div class=\'tree-qtip-cell-caption\'><b>Typ:</b></div>' + rootType;
	

	var root = new Ext.tree.TreeNode({
		text: 'JSON',
		value: '|||JSON|ROOT|NODE|||',
		draggable:false,
		leaf: false,
		id:'json_tree_root_node',
		iconCls: "ico_" + rootType,
		type: rootType,
		qtip: qTip,
		children: jsonTreeObject,
		expanded: true
	});

	JSONxEdit_jsonTree.setRootNode(root);
	JSONxEdit_jsonTree.getLoader().load( JSONxEdit_jsonTree.root );
	JSONxEdit_jsonTree.enable();

	disableEditor();
};

var saveTreeToJsonString = function () {
	var rootNode = JSONxEdit_jsonTree.getRootNode();
	var jsonString = "";

	if ( rootNode.attributes.isArray )
		jsonString += "[\n";
	else
		jsonString += "{\n";

	jsonString = buildJSONStringFromTree ( rootNode, jsonString, 0 );

	if ( rootNode.attributes.isArray )
		jsonString += "]\n";
	else
		jsonString += "}\n";

	if (jsonString.substring(0,1) == "\t")
		jsonString = jsonString.substring(1, jsonString.length);
	if (jsonString.substring(0,1) == "\t")
		jsonString = jsonString.substring(1, jsonString.length);

	Ext.getCmp("JSONxEdit_FormPanel_TextArea_jsonString").setValue( jsonString );
};